<?php

require_once 'GlobalSettings.php';
require_once 'resource/UploadManager.php';
require_once 'user/UserManager.php';
require_once 'user/AccessControlManager.php';
require_once 'LerniBasicController.php';

class UploaderController extends LerniBasicController
{
	
	public function init()
	{
	   parent::init();
	   $this->defaultSessionName = GlobalSettings::UPLOAD_SESSON;
	}
	
    public function indexAction()
    {  
	   if(GlobalSettings::RELEASE_MODE)
	   {
	      $this->gotoRootPage();
		  return;
	   }
	   $this->view->showForm = true;        
	   $this->view->message = "\nUploadProgress=" . Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable();
       $this->view->message .= "\nApC Support=" . Zend_File_Transfer_Adapter_Http::isApcAvailable();		
    }
	
	public function progressAction()
	{	  	   
	    /*we do not need this now
		if(this->getRequest()->getParam("format") == "jspull")
		{
		  $adapter = new Zend_ProgressBar_Adapter_JsPull();
		  Zend_File_Transfer_Adapter_Http::getProgress(array('progress' => $adapter));
		  return;
		}
		*/
		
	    ///we must create a session to ensure that the browser will refresh.
	    $session = $this->getDefaultSession();	
		if(isset($session->errorMessage))
		{
		   $this->view->message = $session->errorMessage;
		   unset($session->errorMessage);
		}
		else
		{
		   $upload  = Zend_File_Transfer_Adapter_Http::getProgress();
		   if($upload['done'])
		   {
		     if($session->finished)///some times apc is done but is not really done.
			 {			 
			   $this->view->message = 100;
			 }
			 else
			 {
			   $this->view->message = 99;
			 }
		   }
		   else
		   {
		     $this->view->message = (int)($upload['current'] * 100 / $upload['total']);			
		   }	
		}
		
		$this->render("progress");		
	}
	
	public function uploadAction()
	{  	  
	   $request = $this->getRequest();
	   $session = $this->getDefaultSession();
	   if(!$request->isPost()) 
	   {		 
	      $message = "Only POST method is allowed.";		 
		  $session->errorMessage = $message;
		  $this->renderError($message);
		  return;
	   }
	 
	  
	  $uploadAdapter = new Zend_File_Transfer_Adapter_Http();
	   
	  $fileInfos = $uploadAdapter->getFileInfo();    
	  $user = $this->getCurrentUser();		  
      set_time_limit(GlobalSettings::MAX_UPLOAD_TIME);

	  $requestInfo = $this->getRequest()->getParams();	
	  
	  foreach ($fileInfos as $file => $info)
	  {			
		if(!$uploadAdapter->isUploaded($file)) 
		{   			
			continue;
		}
		
		$privilege = AccessControlManager::instance()->hasPrivilegeToUpload($user,$info,$requestInfo);
	    if(true !== $privilege)
	    {		  
		   $session->errorMessage = $privilege;
		   $this->view->message = $privilege;		   
	       continue;
	    }	  
		
		$uploadManager = new UploadManager();
		$destFile = $uploadManager->beginUploadFile($user,$info,$requestInfo);
		if(NULL == $destFile)
		{		   
		    $session->errorMessage = "Failed to receive file:\n" . implode('\n',$info); 
		    $this->view->message .= $session->errorMessage;
		    continue;
		}	
		
        $uploadAdapter->addFilter('Rename', $destFile,$file);			
		if(!$uploadAdapter->receive($file))
		{				  		  
		  $session->errorMessage = implode("\n",$uploadAdapter->getMessages());
		  $uploadManager->abortUploadFile();		         
		}
		else
		{
		  try
		  {
		    $uploadManager->endUploadFile();
		  }
		  catch(Exception $e)
		  {
		      $session->errorMessage = $e->getMessage();
		  }
		}
	  }
	  
	  $session->finished = true; 
	  $this->render("index");
	}
}